﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using FindArbitrageConsole.Data;
using FindArbitrageConsole.Data.Model;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace FindArbitrage.Tests.Data
{
    [DeploymentItem(@"..\..\..\mappings.xlsx")]
    public class MockedDataService
    {
        private OleDbConnection Connection { get; set; }
        
        public MockedDataService()
        {
            var pathToFile = @"C:\Users\dima_r\Desktop\unitTest.xlsx";
            //var connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathToFile + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";";
            var connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pathToFile + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=0\";";

            Connection = new OleDbConnection(connStr);
            Connection.Open();

            var query = "SELECT * FROM selection";
            var cmd = Connection.CreateCommand();
            cmd.CommandText = query;
            
            var r = cmd.ExecuteReader();
            var list = new List<string>();
            while (r.Read())
            {
                list.Add(r[0].ToString());
            }
            var odp = new OleDbDataAdapter(query, Connection);

            var ds = new DataSet();
            odp.Fill(ds);
            var b = 5 + 5;
        }

        public IEnumerable<KefData> GetAllMaxKefs()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<KefData> GetAllMaxKefsAfterUpdateTime(DateTime moreThanUpdateTime)
        {
            throw new NotImplementedException();
        }

        public IEnumerable<KefData> GetAllKefs()
        {
            const string query = "SELECT * FROM kef";
            var cmd = Connection.CreateCommand();
            cmd.CommandText = query;
            var r = cmd.ExecuteReader();
            var kefs = new List<KefData>();
            while(r.Read())
                kefs.Add(new KefData()
                             {
                                 Id = Convert.ToInt32(r["id"]),
                                 EventId = Convert.ToInt32(r["event_id"]),
                                 MarketId = Convert.ToInt32(r["market_id"]),
                                 SelectionId = Convert.ToInt32(r["selection_id"]),
                                 BookmakerId = Convert.ToInt32(r["bookmaker_id"]),
                                 KefValue = Convert.ToDecimal(r["kef_value"]),
                                 UpdateTime = Convert.ToDateTime(r["update_time"])
                             });
            return kefs;
        }
        public IEnumerable<KefData> GetAllKefsAfterUpdateTime(DateTime moreThanUpdateTime)
        {
            const string query = "SELECT * FROM kef WHERE update_time > @datetime";
            var cmd = Connection.CreateCommand();
            cmd.CommandText = query;
            cmd.Parameters.Add(new OleDbParameter("datetime", moreThanUpdateTime));

            var r = cmd.ExecuteReader();
            var kefs = new List<KefData>();
            while (r.Read())
                kefs.Add(new KefData()
                {
                    Id = Convert.ToInt32(r["id"]),
                    EventId = Convert.ToInt32(r["event_id"]),
                    MarketId = Convert.ToInt32(r["market_id"]),
                    SelectionId = Convert.ToInt32(r["selection_id"]),
                    BookmakerId = Convert.ToInt32(r["bookmaker_id"]),
                    KefValue = Convert.ToDecimal(r["kef_value"]),
                    UpdateTime = Convert.ToDateTime(r["update_time"])
                });
            return kefs;
        }

        public IEnumerable<int> GetAllEvents()
        {
            var str = "insert into arbitrage(`id`,`start_time`,`update_time`,`end_time`) values(1,'34.34.34','11.11.11','2.2.2.')";
            //var str = "update arbitrage set id = 1";
            var cm = Connection.CreateCommand();
            cm.CommandText = str;
            cm.ExecuteNonQuery();
            return null;
            const string query = "SELECT * FROM kef";
            var cmd = Connection.CreateCommand();
            cmd.CommandText = query;
            var r = cmd.ExecuteReader();
            var kefs = new List<KefData>();
            while (r.Read())
                kefs.Add(new KefData()
                {
                    Id = Convert.ToInt32(r["id"]),
                    EventId = Convert.ToInt32(r["event_id"]),
                    MarketId = Convert.ToInt32(r["market_id"]),
                    SelectionId = Convert.ToInt32(r["selection_id"]),
                    BookmakerId = Convert.ToInt32(r["bookmaker_id"]),
                    KefValue = Convert.ToDecimal(r["kef_value"]),
                    UpdateTime = Convert.ToDateTime(r["update_time"])
                });
            return null;
        }

        public IEnumerable<ExtendedArbitrageLinkData> GetExtendedArbitrageLinks()
        {
            throw new NotImplementedException();
        }

        public void WriteNewArbitrages(List<IEnumerable<ArbitrageLinkData>> listArbitrages)
        {
            throw new NotImplementedException();
        }

        public void UpdateExistingArbitrages(IEnumerable<int> arbitrageIds, DateTime updateTime)
        {
            throw new NotImplementedException();
        }

        public void DeleteIrrelevantArbitrages(DateTime currentTime)
        {
            throw new NotImplementedException();
        }
    }
}
